{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-8">
        <h1>Flask Database Integration with SQLAlchemy</h1>
        <p class="lead">Chapter 5: Database Operations</p>

        <p>This demo showcases database integration techniques.</p>

        <h3>Topics Covered:</h3>
        <ul>
            <li>Flask-SQLAlchemy integration</li>
            <li>Model definition with relationships</li>
            <li>CRUD operations (Create, Read, Update, Delete)</li>
            <li>Querying with SQLAlchemy ORM</li>
            <li>Database migrations with Flask-Migrate</li>
            <li>One-to-many and many-to-many relationships</li>
        </ul>

        <style>
            .list-group .list-group-item {
                background-color: #f8f9ff;
                border-radius: 12px;
                margin-bottom: 0.75rem;
                border: 1px solid #e0e7ff;
                transition: transform 0.15s ease, box-shadow 0.15s ease;
            }

            .list-group .list-group-item:hover {
                transform: translateY(-2px);
                box-shadow: 0 10px 20px rgba(79, 70, 229, 0.15);
                background-color: #eef2ff;
            }
        </style>

        <h3>Demo Sections:</h3>
        <div class="list-group">
            <a class="list-group-item list-group-item-action" href="{{ url_for('users') }}">
                <h5>User Management</h5>
                <p class="mb-1">View, create, and manage users</p>
            </a>
            <a class="list-group-item list-group-item-action" href="{{ url_for('posts') }}">
                <h5>Post Management</h5>
                <p class="mb-1">View, create, and manage posts</p>
            </a>
            <a class="list-group-item list-group-item-action" href="{{ url_for('create_user') }}">
                <h5>Create User</h5>
                <p class="mb-1">Add a new user to the database</p>
            </a>
            <a class="list-group-item list-group-item-action" href="{{ url_for('create_post') }}">
                <h5>Create Post</h5>
                <p class="mb-1">Create a new post associated with a user</p>
            </a>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5>Database Information</h5>
            </div>
            <div class="card-body">
                <p><strong>Database Engine:</strong> SQLite</p>
                <p><strong>Database Location:</strong> {{ db_path }}</p>
                <p><strong>Users Count:</strong> {{ users_count }}</p>
                <p><strong>Posts Count:</strong> {{ posts_count }}</p>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h5>SQLAlchemy Best Practices</h5>
            </div>
            <div class="card-body">
                <ul>
                    <li>Use models to represent database tables</li>
                    <li>Define relationships between models</li>
                    <li>Use Flask-Migrate for schema changes</li>
                    <li>Handle database sessions properly</li>
                    <li>Implement proper error handling</li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}